package com.xtravel.common.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.AttributeSet;
import android.view.View;

public class XSlidePanelLayout extends SlidingPaneLayout {
	protected View mMenuPanel;
	protected float mSlideOffset;
	protected boolean isCustomable = false;

	public XSlidePanelLayout(Context context) {
		this(context, null);
	}

	public XSlidePanelLayout(Context context, AttributeSet attrs) {
		this(context, attrs, -1);
	}

	@SuppressLint("NewApi")
	public XSlidePanelLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			isCustomable = true;

			setPanelSlideListener(new SimplePanelSlideListener() {
				@Override
				public void onPanelSlide(View panel, float slideOffset) {
					mSlideOffset = slideOffset;
					final float scaleLeftX = 1 - 0.3f * (1 - slideOffset);
					final float scaleLeftY = 1 - 0.1f * (1 - slideOffset);
					mMenuPanel.setPivotX(0);
					mMenuPanel.setPivotY(mMenuPanel.getHeight() / 2f);
					mMenuPanel.setScaleX(scaleLeftX);
					mMenuPanel.setScaleY(scaleLeftY);

					final float scale = 1 - 0.15f * slideOffset;
					panel.setPivotX(0);
					panel.setPivotY(panel.getHeight() / 2.0f);
					panel.setScaleX(scale);
					panel.setScaleY(scale);
				}
			});
			setSliderFadeColor(0);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (isCustomable) {
			dimOnForeground(canvas);
		}
	}

	@Override
	protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
		boolean result = super.drawChild(canvas, child, drawingTime);
//		if (child.getId() == R.id.userSlideMenus) {
//			if (mMenuPanel == null) {
//				mMenuPanel = child;
//				int width = getWidth();
//				int height = getHeight();
//				mMenuPanel.setLayoutParams(new SlidingPaneLayout.LayoutParams(width - 160, height));
//			}
//		}
		if (isCustomable && child == mMenuPanel) {
			dimOnForeground(canvas);
		}
		return result;
	}

	private void dimOnForeground(Canvas canvas) {
		this.setBackgroundColor(Color.argb((int) (0xff * (mSlideOffset)), 17, 55, 96));
	}
}
